import { mergeOptions } from "./mergeOptions";


export default function initGlobalAPI(Vue) {
  Vue.options = {};
  Vue.options.components = {}; // 存储全局组件
  Vue.options._base = Vue; // 组件使用到它

  Vue.mixin = function (options) {

    this.options = mergeOptions(this.options, options);
    return this;
  }

  Vue.component = function (id, definition) {
    // console.log("component", id, definition)
    definition.name = definition.name || id; // 标识

    definition = this.options._base.extend(definition); // definition = Vue.extend(definition);
    this.options.components[id] = definition;
  }

  // Vue.extend 中 data 必须是函数
  // Vue.extend 把对象转换成一个类，该类继承了父类 Vue
  Vue.extend = function (extendOptions) {
    // console.log("extend", extendOptions)
    const Super = this;
    const Sub = function VueComponent(options) {
      this._init(options);
    };

    Sub.prototype = Object.create(Super.prototype);
    Sub.prototype.constructor = Sub;
    Sub.options = mergeOptions(Super.options, extendOptions);
    // console.log(this.options)

    // 扩展其他属性 ... 比如 Sub.mixin 等
    return Sub;
  }
}
